<template>
    <basic-container >
      <avue-crud :option="option"
                 :table-loading="loading"
                 :data="data"
                 :page="page"
                 :permission="permissionList"
                 :before-open="beforeOpen"
                 v-model="form"
                 ref="crud"
                 @row-update="rowUpdate"
                 @row-save="rowSave"
                 @row-del="rowDel"
                 @search-change="searchChange"
                 @search-reset="searchReset"
                 @selection-change="selectionChange"
                 @current-change="currentChange"
                 @size-change="sizeChange"
                 @on-load="onLoad"
                 v-if="showMain"
      >
        <template slot="menuLeft">
          <el-button type="danger"
                     size="small"
                     icon="el-icon-delete"
                     plain
                     v-if="permission.jkcs_delete"
                     @click="handleDelete">删 除
          </el-button>
        </template>
        <template slot-scope="{type,size,row,index}"  slot="menuBtn">
          <el-dropdown-item divided @click.native="show1View(row,index)">查看记录</el-dropdown-item>
          <el-dropdown-item divided @click.native="show2View(row,index)">查看评价</el-dropdown-item>
        </template>
      </avue-crud>
      <Jkcsitem v-bind:csId="csId"   :showMainView="showMainView" v-if="(!showMain)&&show1" ref="eps1"></Jkcsitem>
      <Jkcsevaluate v-bind:csId="csId"    :showMainView="showMainView" v-if="(!showMain)&&show2" ref="eps2"></Jkcsevaluate>
    </basic-container>


</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/service/jkcs";
  import {mapGetters} from "vuex";

  import Jkcsevaluate from "@/views/service/jkcsevaluate";

  import Jkcsitem from "@/views/service/jkcsitem";


  export default {
    components: {
      Jkcsitem,
      Jkcsevaluate
    },
    data() {
      return {
        showMain:true,
        show1:false,
        show2:false,
        csId:'',
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          menuType:'menu',
          menuBtnTitle:'更多操作',
          column: [
            {
              label: "主键",
              prop: "id",
              rules: [{
                required: true,
                message: "请输入主键",
                trigger: "blur"
              }],
              hide:true,
              display:false
            },
            {
              label: "餐食日期",
              prop: "csTime",
              type: "datetime",
              format: "yyyy-MM-dd",
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              search: true,
              rules: [{
                required: true,
                message: "请输入餐食日期",
                trigger: "blur"
              }]
            },
            {
              label: "学校名称",
              prop: "schoolId",
              type: "select",
              search:false,
              rules: [{
                required: true,
                message: "请输入学校名称",
                trigger: "blur"
              }],
              dicUrl: "/api/school-service/school/select",
              props: {
                label: "xxName",
                value: "id"
              },
            },
            {
              label: "星期",
              prop: "stId",
              rules: [{
                required: true,
                message: "请输入星期",
                trigger: "blur"
              }]
            },
            {
              label: "评分",
              prop: "csScore",
              rules: [{
                required: false,
                message: "请输入评分",
                trigger: "blur"
              }],
              type:"rate",
              hide: true,
              addDisabled:true,
              editDisabled:true
            },
            // {
            //   label: "餐段",
            //   prop: "stId",
            //   rules: [{
            //     required: true,
            //     message: "请选择餐段",
            //     trigger: "blur"
            //   }],
            //   type: "select",
            //   dicData:[{
            //       label: "早餐",
            //       value: 1
            //     },{
            //       label: "中餐",
            //       value: 2
            //     },{
            //       label: "晚餐",
            //       value: 3
            //     }],
            //     value:1
            // },
            // {
            //   label: "食堂名称",
            //   prop: "stName",
            //   rules: [{
            //     required: true,
            //     message: "请输入食堂名称",
            //     trigger: "blur"
            //   }]
            // },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission","JSuserInfo"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.jkcs_add, false),
          viewBtn: this.vaildData(this.permission.jkcs_view, false),
          delBtn: this.vaildData(this.permission.jkcs_delete, false),
          editBtn: this.vaildData(this.permission.jkcs_edit, false)
        };
      },
      schoolId(){
        return this.JSuserInfo.schoolId
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {

      show1View(row){
        this.csId = row.id;
        //this.$refs.eps.onLoad({currentPage:1,pageSize:10});
        this.showMain = false;
        this.show1 = true;
        this.show2 = false;
      },
      show2View(row){
        this.csId = row.id;
        //this.$refs.eps.onLoad({currentPage:1,pageSize:10});
        this.showMain = false;
        this.show2 = true;
        this.show1 = false;
      },
      showMainView(){
        this.showMain = true;
        this.show1 = false;
        this.show2 = false;
      },
      goBack(){
        this.showMainView();
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        params.SchoolId = this.schoolId
        params.tenant_id = ''
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.totalItems;
          this.data = data.items;
          this.loading = false;
          this.selectionClear();
        });
        const school = this.findObject(this.option.column, "schoolId");
        school.hide = true;
      }
    }
  };
</script>

<style>
</style>
